﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys.Farmings;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.Traceabilitys.Farmings
{
    /// <summary>
    /// 获取保健信息命令处理
    /// </summary>
    public class GetFarmingHealthCommandHandlter : IRequestHandler<GetFarmingHealthCommand, ApiResult<List<FarmingHealthcareModel>>>
    {

        /// <summary>
        /// 保健信息仓储
        /// </summary>
        private readonly IBaseRepository<FarmingHealthcareModel> farmingHealthRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="farmingHealthRep">保健信息仓储</param>
        public GetFarmingHealthCommandHandlter(IBaseRepository<FarmingHealthcareModel> farmingHealthRep)
        {
            this.farmingHealthRep = farmingHealthRep;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<List<FarmingHealthcareModel>>> Handle(GetFarmingHealthCommand request, CancellationToken cancellationToken)
        {


            //根据溯源编号获取保健信息
            var health= farmingHealthRep.GetAll().AsNoTracking().Where(x=>x.ProductId==request.ProductId&&!x.IsDel).ToList();

            return new ApiResult<List<FarmingHealthcareModel>>
            {
                code = ResultEnumCode.Ok,
                data = health,
                message = "获取保健信息成功"
            };
        }
    }
}
